package com.block.datapush.form.item;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.common.util.LocalDateUtil;
import com.block.datapush.form.service.IFormPushHandle;
import com.block.datapush.form.item.util.YunMiaoDaoUtil;
import com.block.datapush.model.dto.FormPushResultDTO;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoHis;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class YunMiaoDaoPushHandleService  implements IFormPushHandle {

    private static final Logger log = LogFactory.getCommLog();

    @Override
    public boolean queryMatchResult(FormInfo formInfo, MemberExtInfo memberExtInfo) {
        return true;
    }

    @Override
    public FormPushResultDTO pushMemberInfo(FormInfo formInfo, MemberExtInfo memberExtInfo, String orderNo, JSONObject extObject) {
        FormPushResultDTO pushResultDTO = new FormPushResultDTO();
        pushResultDTO.setOrderNo(orderNo);
        try {

            String requestUrl = StrUtil.format(formInfo.getRequestUrl(), formInfo.getApiKey());

            /**
             * { "手机号": "123156719021",
"创建时间": "2022-10-13 11:34:10",
"真实姓名": "样例",
"城市": "上海市",
"省份": "上海市",
"年龄": 25,
"性别": "女",
"贷款金额": 10,
"职业信息": "上班族",
"月收入": "3000-5000",
"收入形式": "自存",
"工作年限": "0-6个月",
"社保缴纳": "无社保",
"公积金缴纳": "无公积金",
"房产情况": "无房产",
"车辆情况": "无车辆",
"保险保单": "无保单",
"信用卡": "无",
"京东白条": "无",
"芝麻分": "600分以下",
"花呗额度": "无",
"有无逾期": "无" }
             */
            Map<String, Object> bizParam = new HashMap<>();
            bizParam.put("手机号", memberExtInfo.getMobile());
            bizParam.put("创建时间", LocalDateUtil.getNowDateStr(LocalDateUtil.TOTAL_DATE_TIME));
            bizParam.put("真实姓名", memberExtInfo.getUsername());
            bizParam.put("城市", YunMiaoDaoUtil.getCity(memberExtInfo.getCity(), memberExtInfo.getProvince()));
            bizParam.put("省份", YunMiaoDaoUtil.getProvince(memberExtInfo.getProvince()));
            bizParam.put("年龄", IdcardUtil.getAgeByIdCard(memberExtInfo.getIdCard()));
            bizParam.put("性别", IdcardUtil.getGenderByIdCard(memberExtInfo.getIdCard())==1?"男":"女");

            bizParam.put("职业信息", "-");
            bizParam.put("月收入", "5000-1万");
            bizParam.put("收入形式", "银行卡");
            bizParam.put("工作年限", "12个月以上");
            bizParam.put("信用卡", "-");

            bizParam.put("贷款金额", YunMiaoDaoUtil.getLoanAmount(memberExtInfo.getLoanAmount()));
            bizParam.put("社保缴纳", YunMiaoDaoUtil.getShebao(memberExtInfo.getSocialInsurance()));
            bizParam.put("公积金缴纳", YunMiaoDaoUtil.getGjj(memberExtInfo.getGjj()));
            bizParam.put("房产情况", YunMiaoDaoUtil.getHouse(memberExtInfo.getHouse()));
            bizParam.put("车辆情况", YunMiaoDaoUtil.getCar(memberExtInfo.getCar()));
            bizParam.put("保险保单", YunMiaoDaoUtil.getBaodan(memberExtInfo.getInsurance()));
            bizParam.put("京东白条", YunMiaoDaoUtil.getBaitiao(memberExtInfo.getBaitiao()));
            bizParam.put("芝麻分", YunMiaoDaoUtil.getZhima(memberExtInfo.getZhimaScore()));
            bizParam.put("花呗额度", YunMiaoDaoUtil.getHuabei(memberExtInfo.getHuabei()));
            bizParam.put("有无逾期", YunMiaoDaoUtil.getYuqi(memberExtInfo.getYuqi()));

            Map<String, Object> formMap = new HashMap<>();
            formMap.put("version", "1.0");
            formMap.put("merchant_no", formInfo.getApiKey());
            formMap.put("form_data", bizParam);

            String data = YunMiaoDaoUtil.encryptByPublicKey(JSON.toJSONString(formMap), formInfo.getPubKey());
            String sign = YunMiaoDaoUtil.signByPrivateKey(JSON.toJSONString(formMap), formInfo.getPriKey());

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("data", data);
            paramMap.put("sign", sign);

            log.info("【云秒到】导单开始【requestUrl】{}【bizParam】{}【paramMap】{}",
                    requestUrl, JSON.toJSONString(bizParam), JSON.toJSONString(paramMap));
            String result = HttpUtil.createPost(requestUrl)
                    .header("Content-Type", "application/json")
                    .body(JSON.toJSONString(paramMap))
                    .execute().body();
//            String result = "";
            log.info("【云秒到】导单结束【result】{}", result);

            JSONObject resultObject = JSON.parseObject(result);

            String code_no = resultObject.getString("code_no");
            String code_msg = resultObject.getString("code_msg");

            pushResultDTO.setResult(result);
            pushResultDTO.setCode(String.valueOf(code_no));
            pushResultDTO.setMessage(code_msg);
            pushResultDTO.setParamContent(JSON.toJSONString(formMap));
            if (StrUtil.equals(code_no, "0")){
                pushResultDTO.setSuccess(true);
            }else{
                pushResultDTO.setSuccess(false);
            }
            return pushResultDTO;
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return pushResultDTO;
    }

    @Override
    public void afterPushHandle(FormInfoHis formInfoHis) {

    }
}
